package net.huashitong.Indicators.service;

import com.sirdc.modules.paas.api.common.CommonRequest;
import com.sirdc.modules.paas.api.common.Constants;
import com.sirdc.modules.paas.api.mo.MoInfo;
import com.sirdc.modules.paas.api.mo.MoRequest;
import com.sirdc.modules.paas.api.mo.MoResponse;
import com.sirdc.modules.paas.api.mt.MtRequest;
import com.sirdc.modules.paas.api.mt.MtResponse;
import com.sirdc.modules.paas.api.report.ReportInfo;
import com.sirdc.modules.paas.api.report.ReportRequest;
import com.sirdc.modules.paas.api.report.ReportResponse;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SmsProcess {
    public static final String APP_CODE = "2059";
    public static final String APP_KEY = "smases1469";

    /**
     * 发送短信
     * @param phoneNumbers 手机号码
     * @return
     */
    public Map<String,Object> SendMts(String phoneNumbers, String smsContent){
        Map<String, Object> map = new HashMap<>();
        try {

            //请求路径
            String url = Constants.DEFAULT_URL+"/sendMts.xml";
            MtRequest request = new MtRequest(APP_CODE, APP_KEY, url);

            String sendTime = DateForIndUtils.getCurDate("yyyy-MM-dd hh:mm:ss");//发送时间
            MtResponse mtResponse = request.send(phoneNumbers, smsContent, sendTime);
            //结果
            map.put("MtAmount", mtResponse.getMtAmount());
            map.put("StateCode", mtResponse.getStateCode());
            map.put("StateIntro", mtResponse.getStateIntro());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 接收短信
     * @param amount 接收条数
     * @return
     */
    public Map<String,Object> RecvMos(String amount){
        Map<String,Object> map = new HashMap<>();
        try{
            String url = "https://csm.xm.gov.cn:8443/smas-pass-interface-server/smas/recvMos.xml";
            MoRequest request = new MoRequest(APP_CODE, APP_KEY, url);// 创建接收短信请求对象
            if(StringUtils.isNotBlank(amount)){
                amount = "2";//默认接收条数
            }
            MoResponse moResponse = request.receive(amount);
            List<MoInfo> moList = moResponse.getMoList();
            //结果
            map.put("MoAmount", moResponse.getMoAmount());
            map.put("StateCode", moResponse.getStateCode());
            map.put("StateIntro", moResponse.getStateIntro());
            if (moList == null) {
                System.out.println("MoList is null!");
            } else {
                for (MoInfo moInfo : moList) {
                    System.out.println("phoneNum:"+moInfo.getPhoneNumber()+"  content:"+moInfo.getContent());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 接收短信回执
     * @return
     */
    public Map<String,Object> RecvRpts(){
        Map<String,Object> map = new HashMap<>();
        try {
            String url = "https://csm.xm.gov.cn:8443/smas-pass-interface-server/smas/recvRpts.xml";
            // 创建接收短信回执请求对象
            ReportRequest request = new ReportRequest(APP_CODE, APP_KEY, url);
            String amount = "100";
            ReportResponse reportResponse = request.report(amount);
            List<ReportInfo> reportList = reportResponse.getReportList();

            //结果
            map.put("MoAmount", reportResponse.getAmount());
            map.put("StateCode", reportResponse.getStateCode());
            map.put("StateIntro", reportResponse.getStateIntro());

            //输出
            System.out.println("Amount:" + reportResponse.getAmount());
            System.out.println("StateCode:" + reportResponse.getStateCode());
            System.out.println("StateIntro:" + reportResponse.getStateIntro());
            if (reportList == null) {
                System.out.println("ReportList is null!");
            } else {
                for (ReportInfo reportInfo : reportList) {
                    System.out.println("phoneNum:"+reportInfo.getPhoneNumber());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
